package com.istock.state.mock;

import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.time.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import com.alibaba.fastjson.JSON;
import com.istock.mock.ApplyInfoStatusEnum;
import com.istock.state.mock.dao.ApplyInfoMapper;
import com.istock.state.mock.model.ApplyInfo;
import com.istock.state.mock.model.ApplyInfoExample;

public class ApplyInfoService {

	@Autowired
	private ApplyInfoMapper applyInfoMapper;
	
	private final Integer TYPE_PAY = 1;
	private final Integer TYPE_GOODS = 2;
	
	@Transactional
	public ApplyInfo createPayApplyWithAmount(Long requestId , BigDecimal amount,ApplyInfoStatusEnum status) {
		ApplyInfo applyInfo = new ApplyInfo();
		applyInfo.setRequestId(requestId);
		Map<String , Object> param = new HashMap<String , Object>();
		param.put("amount", amount);
		applyInfo.setType(TYPE_PAY);
		applyInfo.setStatus(status.getValue());
		applyInfo.setContent(JSON.toJSONString(param));
		applyInfo.setUpdateTime(new Date());
		applyInfoMapper.insertSelective(applyInfo);
		return applyInfo;
	}
	
	@Transactional
	public ApplyInfo createGoodsApply(Long requestId) {
		ApplyInfo applyInfo = new ApplyInfo();
		applyInfo.setRequestId(requestId);
		Map<String , Object> param = new HashMap<String , Object>();
		applyInfo.setContent(JSON.toJSONString(param));
		applyInfo.setType(TYPE_GOODS);
		applyInfo.setStatus(ApplyInfoStatusEnum.S.getValue());
		applyInfo.setUpdateTime(new Date());
		applyInfoMapper.insertSelective(applyInfo);
		return applyInfo;
	}
	
	public ApplyInfo searchPayApplyInfo(Long requestId) {
		ApplyInfoExample example = new ApplyInfoExample();
		ApplyInfoExample.Criteria criteria = example.createCriteria();
//		criteria.andIdEqualTo(id);
		criteria.andRequestIdEqualTo(requestId);
		criteria.andTypeEqualTo(TYPE_PAY);
		List<ApplyInfo> applyList = applyInfoMapper.selectByExample(example);
		if(applyList != null && applyList.size()>0) {
			return applyList.get(0);
		}
		return null;
	}
	
	public ApplyInfo searchGoodsApplyInfo(Long requestId) {
		ApplyInfoExample example = new ApplyInfoExample();
		ApplyInfoExample.Criteria criteria = example.createCriteria();
//		criteria.andIdEqualTo(id);
		criteria.andRequestIdEqualTo(requestId);
		criteria.andTypeEqualTo(TYPE_GOODS);
		List<ApplyInfo> applyList = applyInfoMapper.selectByExample(example);
		if(applyList != null && applyList.size()>0) {
			return applyList.get(0);
		}
		return null;
	}
	
	public ApplyInfo findApplyById(Long id) {
		return applyInfoMapper.selectByPrimaryKey(id);
	}
	
	public List<ApplyInfo> findApplyNoHandler(){
		ApplyInfoExample example = new ApplyInfoExample();
		ApplyInfoExample.Criteria criteria = example.createCriteria();
		
		criteria.andUpdateTimeBetween(DateUtils.addMinutes(new Date(), -30), DateUtils.addMinutes(new Date(), -1));
		criteria.andStatusIn(Arrays.asList(ApplyInfoStatusEnum.W.getValue(),ApplyInfoStatusEnum.P.getValue()));
		//这边为了防止单次查询的数据量过多,可以增加orderBy和分页
		return applyInfoMapper.selectByExample(example);
	}
	
	@Transactional
	public void changeStatus(Long id , ApplyInfoStatusEnum status) {
		ApplyInfo applyInfo = new ApplyInfo();
		applyInfo.setId(id);
		applyInfo.setStatus(status.getValue());
		applyInfo.setUpdateTime(new Date());
		applyInfoMapper.updateByPrimaryKeySelective(applyInfo);
	}
}
